home *** CD-ROM | disk | FTP | other *** search
- // hexdump.cpp : Program producing hexdump of input file
- #include <fstream.h>
- #include <iomanip.h>
- #include <ctype.h>
-
- main(int argc, char *argv[])
- {
- istream *fin = &cin; // default input is cin
- ostream *fout = &cout; // default output is cout
- fstream infile, outfile;
-
- if (argc > 1) { // input file specified
- infile.open(argv[1], ios::in | ios::binary);
- if (!infile) {
- cerr << "Error opening input file " << argv[1] << '\n';
- return 1;
- }
- fin = &infile;
- }
-
- char ascii_array[17]; // ascii portion of dump
- ascii_array[16] = 0; // make it null terminated
- int nextc = 0;
-
- // set up for uppercase hex output with '0' fill
- *fout << setiosflags(ios::uppercase) << hex << setfill('0');
-
- // loop till end of file
- while(1) {
- int c = fin->get();
- if (c == EOF) break;
- *fout << setw(2) << c << ' ';
- if (!isprint(c)) c = '.'; // convert nonprintable chars to '.'
- ascii_array[nextc++] = c;
- if (nextc == 16) {
- *fout << ascii_array << '\n';
- nextc = 0;
- }
- }
- // dump out left overs
- if (nextc) {
- for (; nextc<16; nextc++) {
- *fout << "00 ";
- ascii_array[nextc] = '.';
- }
- *fout << ascii_array << '\n';
- }
- return 0;
- }
-
-
-
-
-